﻿define([],function(){
	return {
		//随机颜色
		getColor : function(){
			var r = Math.round(Math.random()*255);
			var g = Math.round(Math.random()*255);
			var b = Math.round(Math.random()*255);
			return "rgb(" + r + "," + g + "," + b + ")";
		},
		
		//获取元素样式
		getStyle : function(elem,attr){
			if(window.getComputedStyle){
				return getComputedStyle(elem,null)[attr];
			}else{
				return elem.currentStyle[attr];
			}
		},
		
		// 通过id名获取元素
		getId : function(id){
			return document.getElementById(id);
		},
		
		// 通过class名获取元素
		getClass : function(classname){
			if(document.getElementsByClassName){
				return document.getElementsByClassName(classname);
			}else{
				var col = [];
				var arr = document.getElementsByTagName("*");
				var l = arr.length;
				for(var i = 0; i < l; i++){
					if(arr[i].className == classname){
						col.push(arr[i]);
					}
				}
				return col;
			}
		},
		
		// 阻止事件冒泡
		stopBubble : function(e){
			if(e.stopPropagation){
				return e.stopPropagation();
			}else{
				return e.cancelBubble = true;
			}
		},
		
		// 阻止文字被选中
		banSelectText : function(){
			if(typeof document.onselectstart != "undefined"){
				document.onselectstart = function(){
					return false;
				}
			}else{
				document.onmousedown = function(){
					return false;
				}
			}
		},
		
		// 事件监听
		addEvtList : function(elem,evt,fn){
			if(elem.addEventListener){
				return elem.addEventListener(evt,fn);
			}else{
				return elem.attachEvent("on" + evt,fn);
			}
		},
		
		// 设置cookie
		setCookie : function(name,value,date){
			if(date){
				var d = new Date();
				d.setDate(d.getDate() + date);
				return document.cookie = name + "=" + value + ";expires=" + d;
			}else{
				return document.cookie = name + "=" + value;
			}
		},
		
		// 获取cookie
		getCookie : function(name){
			if(document.cookie){
				var str = document.cookie;
				var arr = str.split("; ");
				var l = arr.length;
				for(var i = 0; i < l; i++){
					var item = arr[i].split("=");
					if(name == item[0]){
						return item[1];
					}
				}
			}
			return "";
		},
		
		// 删除cookie
		removeCookie : function(name){
			this.setCookie(name,"",-1);
		},
		
		// 拖拽
		drag : function(elem){
			elem.onmousedown = function(e){
				var e = e || window.event;
				var x = e.clientX - elem.offsetLeft;
				var y = e.clientY - elem.offsetTop;
				document.onmousemove = function(e){
					var e = e || window.event;
					var w = document.documentElement.clientWidth || document.body.clientWidth;
					var h = document.documentElement.clientHeight || document.body.clientHeight;
					var mixW = w - elem.offsetWidth;
					var mixH = h - elem.offsetHeight;
					var l = e.clientX - x;
					var t = e.clientY - y;
					if(l < 0){
						l = 0;
					}
					if(l > mixW){
						l = mixW;
					}
					if(t < 0){
						t = 0;
					}
					if(t > mixH){
						t = mixH;
					}
					elem.style.left = l + "px";
					elem.style.top = t + "px";
					return false;
				}
				document.onmouseup = function(){
					document.onmousemove = null;
					document.onmouseup = null;
				}
			}
		},
		
		// 运动函数
		startMove : function(elem,json,fn){
			clearInterval(elem.timer);
			elem.timer = setInterval(function(){
				var flag = true;
				for(var k in json){
					if(k == "zIndex"){
						elem.style[k] = json[k];
					}else if(k == "opacity"){
						var v = getStyle(elem,k)*100;
						var target = json[k]*100;
						var speed = (target - v) / 10;
						if(speed > 0){
							speed = Math.ceil(speed);
						}else{
							speed = Math.floor(speed);
						}
						v += speed;
						elem.style[k] = v / 100;
					}else{
						var v = parseInt(getStyle(elem,k));
						var target = json[k];
						var speed = (target - v) / 10;
						speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
						v += speed;
						elem.style[k] = v + "px";
					}
					if(v != target){
						flag = false;
					}
				}
				if(flag){
					clearInterval(elem.timer);
					if(fn){
						fn();
					}
				}
			},30)
		},
		
		// 通过get发送ajax请求
		ajaxGet : function(url,callback,data){
			var ajax = null;
			if( window.XMLHttpRequest ){
				ajax = new XMLHttpRequest();
			}else{
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}
			if(arguments.length == 3){//表示传递的参数有三个  
				url = url + "?" + data;
			}
			ajax.open("GET",url,true);
			ajax.onreadystatechange = function(){
				if( ajax.readyState == 4 && ajax.status == 200 ){
					if(callback){
						callback(ajax.responseText);//通过函数的调用将服务器处理的结果以参数形式传递给前端
					}	 
				}
			}
			ajax.send(); 
		},
		
		// 通过post发送ajax请求
		ajaxPost : function(url,callback,data){
			var ajax = null;
			if( window.XMLHttpRequest ){
				ajax = new XMLHttpRequest();
			}else{
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			ajax.open("POST",url);
			
			//设置请求头：
			ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			
			ajax.onreadystatechange = function(){
				if(ajax.readyState == 4 && ajax.status == 200){
					callback(ajax.responseText);
				}
			}	
			ajax.send(data);//向服务器端发送数据 用户名
		}
	}
})